using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Text;
using Biolink.Common.Remoting.Sinks;

namespace Biolink.Common.Remoting.Extensions
{
    public class TrafficSink: BaseCustomSink
    {
        private string methodInfo;
        private long requestLength = 0;
        
        protected override void ProcessRequest(IMessage message, ITransportHeaders headers, ref Stream stream, ref object state)
        {
            // MessageDetails.DumpDetails(message,  "Income Request On Server", headers);
            TrafficStatisticConfiguration.UpdateTotal(1, 0, 0);

            
            if (stream != null)
            {
                MemoryStream copy = new MemoryStream();
                byte[] buffer = new byte[1024];
                while (true)
                {
                    int n = stream.Read(buffer, 0, buffer.Length);
                    if (n <= 0)
                        break;

                    copy.Write(buffer, 0, n);
                }

                requestLength = copy.Length;
                stream = copy;
                stream.Position = 0;

                TrafficStatisticConfiguration.UpdateTotal(0, 0, requestLength);
            }
        }

        protected override void ProcessResponse(IMessage message, ITransportHeaders headers, ref Stream stream, object state)
        {
            // MessageDetails.DumpDetails(message, "Outgoing Response On Server", headers);
            long sent = 0;

            if (stream != null)
            {
                if (stream.CanSeek)
                {
                    sent = stream.Length;
                    TrafficStatisticConfiguration.UpdateTotal(0, sent, 0);

                }
            }

            string about = null;
            IMethodMessage rm = message as IMethodMessage;
            if (rm != null)
            {
                
                string t = rm.TypeName;
                string typeName = t;
                string[] tarr = t.Split(new char[] { ','});
                if (tarr.Length > 1)
                {
                    typeName = tarr[0];
                }


                string mn = string.IsNullOrEmpty(rm.MethodName) ? String.Empty : rm.MethodName;
                StringBuilder methodBuilder = new StringBuilder(mn);
                Type[] sign = rm.MethodSignature as Type[];
                if (sign != null)
                {
                    methodBuilder.Append("(");
                    for(int i=0; i<sign.Length; i++)
                    {
                        if (i > 0)
                            methodBuilder.Append(", ");

                        methodBuilder.Append(sign[i].Name);
                    }
                    methodBuilder.Append(")");
                }

                string methodName = methodBuilder.ToString();
                TrafficStatisticConfiguration.Update(typeName, methodName, sent, requestLength);

#if DEBUG && FALSE
                            Trace.WriteLine("Response Message " + typeName + "::" + methodName);
#endif
            }
        }


    }
}